<template>
    <div class="Class">
        <el-row>
            <el-col :span="24">
                <div class="grid-content bg-purple-dark">
                    <el-form :inline="true" :model="queryParams" class="menu-form-inline">
                        <el-form-item label="年级">
                            <el-select v-model="queryParams.grade" placeholder="年级"
                                @change="getClassByGrade(queryParams.grade)">
                                <el-option v-for="item in gradeOptions" :key="item.value" :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item label="班级">
                            <el-select v-model="queryParams.className" placeholder="班级">
                                <el-option v-for="item in classOptions" :key="item.className" :label="item.className+'班'"
                                    :value="item.className">
                                </el-option>
                            </el-select>
                        </el-form-item>

                        <el-form-item>
                            <el-button type="primary" icon="el-icon-search"
                                @click="findShClassPagination()">搜索</el-button>
                            <el-button icon="el-icon-refresh" @click="resetForm()">重置</el-button>

                            <el-button type="primary" @click="(dialogVisible = true), (title = '新增班级'), clearDialog()"
                                icon="el-icon-plus" plain style="margin-left: 700px;">新增班级</el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </el-col>
        </el-row>

        <el-row>
            <el-col :span="24">
                <div class="grid-content bg-purple-dark">
                    <el-table :data="tableData" border height="350">
                        <el-table-column prop="" label="班级名称" align="center">
                            <template slot-scope="scope">
                                <span v-if="scope.row.grade == 1">一年级</span>
                                <span v-if="scope.row.grade == 2">二年级</span>
                                <span v-if="scope.row.grade == 3">三年级</span>
                                <span v-if="scope.row.grade == 4">四年级</span>
                                <span v-if="scope.row.grade == 5">五年级</span>
                                <span v-if="scope.row.grade == 6">六年级</span>
                                <span>( {{ scope.row.className }} )班</span>
                            </template>
                        </el-table-column>
                        <el-table-column align="center" label="操作" width="500rpx">
                            <template slot-scope="scope">
                                <el-button @click="toUpdateShClass(scope.row)" type="text" size="small">编辑</el-button>
                                <el-button @click="delShClass(scope.row)" type="text" size="small">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </el-col>
        </el-row>
        <el-row class="footer">
            <el-col :span="24">
                <div class="grid-content bg-purple-dark">
                    <div class="block">
                        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                            :current-page="pageNum" :page-sizes="[5, 10, 20, 30, 50]" :page-size="pageSize"
                            layout="total, sizes, prev, pager, next, jumper" :total="total">
                        </el-pagination>
                    </div>
                </div>
            </el-col>
        </el-row>

        <!-- 新增弹框 -->
        <el-dialog :title="title" :visible.sync="dialogVisible" width="500px">
            <el-form :model="shClass" :rules="rules" ref="shClass" label-width="100px" class="demo-ruleForm">
                <el-form-item label="年级">
                    <el-select v-model="shClass.grade" placeholder="年级">
                        <el-option v-for="item in gradeOptions" :key="item.value" :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="班号" prop="className">
                    <el-input v-model="shClass.className"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="ifDialog()">确 定</el-button>
                <el-button @click="(dialogVisible = false), clearDialog()">取 消</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import axios from "axios";
import { getToken } from '@/utils/auth';

export default {
    name: "Class",
    data() {
        return {
            tableData: [],
            queryParams: {
                grade: "",
                className: ""
            },
            total: 40,
            pageSize: 5,
            pageNum: 1,
            title: "",
            dialogVisible: false,
            shClass: {
                id: "",
                grade: "",
                className: "",
                createTime: "",
                createBy: "",
                updateTime: "",
                updateBy: ""
            },
            gradeOptions: [
                {
                    value: 1,
                    label: "一年级",
                },
                {
                    value: 2,
                    label: "二年级",
                },
                {
                    value: 3,
                    label: "三年级",
                },
                {
                    value: 4,
                    label: "四年级",
                },
                {
                    value: 5,
                    label: "五年级",
                },
                {
                    value: 6,
                    label: "六年级",
                },
            ],
            categoryIds: [],
            rules: {
                className: [
                    { required: true, message: "请输入班号", trigger: "blur" },
                ],
            },
            dialogVisible: false,
            disabled: false,
            classOptions: []
        };
    },
    created() {
        this.findShClassPagination();
    },
    methods: {
        // 班级列表分页查询
        findShClassPagination() {
            axios
                .post(
                    "/api/manager/class/findShClassPagination?pageNum=" +
                    this.pageNum +
                    "&pageSize=" +
                    this.pageSize,
                    this.queryParams
                )
                .then((res) => {
                    console.log(res)
                    // console.log(res)
                    this.tableData = res.data.data.items;
                    this.total = res.data.data.totalElements;
                });
        },
        // 每页条数改变时调用部门列表分页查询
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
            this.pageSize = val;
            this.findShClassPagination();
        },
        // 页码改变时调用部门列表分页查询
        handleCurrentChange(val) {
            console.log(val);
            this.pageNum = val;
            this.findShClassPagination();
        },
        // dialog 表单提交后清空表单
        clearDialog() {
            for (let key in this.shClass) {
                if (this.shClass.hasOwnProperty(key)) {
                    // 确保属性是对象自身的，而不是继承的
                    this.shClass[key] = ""; // 或者设置为 null
                }
            }
        },
        // 条件查询信息清空 重置
        resetForm() {
            this.queryParams.className = "";
            this.queryParams.grade = "";
            this.findShClassPagination();
        },
        // 班级删除
        delShClass(row) {
            axios.get("/api/manager/class/deleteShClassById?id=" + row.id).then((res) => {
                console.log(res.data);
                if (res.data.code == 200) {
                    this.$message({
                        message: "恭喜你," + res.data.message,
                        type: "success",
                    });
                    this.findShClassPagination();
                } else {
                    this.$message.error(res.data.message);
                }
            });
        },
        // 班级新增
        addShClass() {
            this.shClass.createBy = localStorage.getItem('nickName');
            this.shClass.updateBy = localStorage.getItem('nickName');
            axios.post("/api/manager/class/addShClass", this.shClass).then((res) => {
                if (res.data.code == 200) {
                    this.$message({
                        message: "恭喜你，" + res.data.message,
                        type: "success",
                    });
                    this.dialogVisible = false;
                    this.clearDialog();
                    this.findShClassPagination();
                } else {
                    this.$message.error(res.data.message);
                }
            });
        },
        // 班级回显
        toUpdateShClass(row) {
            this.title = "修改班级";
            axios.post("/api/manager/class/getShClassById?id=" + row.id).then((res) => {
                if (res.data.code == 200) {
                    console.log(res);
                    this.shClass = res.data.data;
                    this.dialogVisible = true;
                } else {
                    this.$message.error(res.data.message);
                }
            });
        },
        // 班级修改
        updateShClass() {
            this.shClass.updateBy = localStorage.getItem('nickName');
            axios.post("/api/manager/class/updateShClassById", this.shClass).then((res) => {
                if (res.data.code == 200) {
                    this.$message({
                        message: "恭喜你，" + res.data.message,
                        type: "success",
                    });
                    this.dialogVisible = false;
                    this.clearDialog();
                    this.findShClassPagination();
                } else {
                    this.$message.error(res.data.message);
                }
            });
        },
        // 判断 title 提交 新增/修改 方法
        ifDialog() {
            if (this.title == "新增班级") {
                this.addShClass();
            }
            if (this.title == "修改班级") {
                this.updateShClass();
            }
        },
        getClassByGrade(grade) {
            console.log(grade)
            axios.post("/api/manager/class/getShClassList?grade=" + grade).then((res) => {
                this.classOptions = res.data.data;
                console.log(res.data)
            });
        }

    },

};
</script>

<style scoped>
.el-row {
    margin-top: 10px;
}

.footer {
    margin-top: 20px;
    text-align: right;
}
</style>